package com.jb.workflow.app.bean;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.jb.workflow.app.client.WfUtil;

import net.oschina.app.AppException;
import net.oschina.app.bean.Entity;
import net.oschina.app.common.StringUtils;

/**
 * 
 * @author hwang
 * 
 * 新增是否需要司机发货属性
 * 如果不需要司机发货：comment结束时候是三个
 * 需要司机发货（默认）：comment结束时候是5个
 * 
 * 库房提交任务之后：Comment由一个变成两个，所以可以根据Comment数量来判断当前流程进行到哪一步了
	1个comment：库房接单提货 开始
	2个comment：客服安排司机发货 开始
	3个comment：司机发货 开始
	4个comment：客服回传发货单 开始
	5个comment：结束
	
 */
public class ProcessInst extends Entity {
	
	//for order workflow
	private String customer;
	private String orderId;
	private boolean needDriver = true;
	//private String comment;
	//存储每个任务完成的时间
	private List<String> taskComment = new ArrayList<String>();  
	private List<String> taskCompleteTime = new ArrayList<String>(); 
	
	private String url;
	private String processDefinitionId;//processDefinitionUrl
	private String processDefinitionUrl;
	private String activityId;
	private boolean completed;
	private String name;
	private String step;
	private String startDate;
	private String endDate;
	private String startUserId;
	private boolean hasDetail;
	
	public boolean isHasDetail() {
		return hasDetail;
	}

	public void setHasDetail(boolean hasDetail) {
		this.hasDetail = hasDetail;
	}

	public String getStartDate() {
		return startDate;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public String getStartUserId() {
		return startUserId;
	}

	public void setStartUserId(String startUserId) {
		this.startUserId = startUserId;
	}

	public String getStep() {
		return step;
	}

	public void setStep(String step) {
		this.step = step;
	}

	public void setId(int id) {
		this.id = id;
	}
	
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String getProcessDefinitionId() {
		return processDefinitionId;
	}
	public void setProcessDefinitionId(String processDefinitionId) {
		this.processDefinitionId = processDefinitionId;
		this.name = WfUtil.getProcessName(processDefinitionId);
	}
	public String getProcessDefinitionUrl() {
		return processDefinitionUrl;
	}
	public void setProcessDefinitionUrl(String processDefinitionUrl) {
		this.processDefinitionUrl = processDefinitionUrl;
	}
	public String getActivityId() {
		return activityId;
	}
	public void setActivityId(String activityId) {
		this.activityId = activityId;
	}
	public boolean isCompleted() {
		return completed;
	}
	public void setCompleted(boolean completed) {
		this.completed = completed;
	}
	public boolean getCompleted() {
		return completed;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getName() {
		return name;
	}
	
	public String getOrderId() {
		return orderId;
	}
	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}
	public String getCustomer() {
		return customer;
	}
	public void setCustomer(String customer) {
		this.customer = customer;
	}
	
	public boolean getNeedDriver() {
		return needDriver;
	}
	public void setNeedDriver(boolean needDriver) {
		this.needDriver = needDriver;
	}
	
	public String getComment() {
		if (!taskComment.isEmpty()) {
			return taskComment.get(taskComment.size()-1);
		}
		return "";
	}
	public List<String> getCompleteTime() {
		return taskCompleteTime;
	}
	
	//订单工作流专用，设置订单号，客户，备注
	public void setOrderData(InputStream inputStream) throws IOException, AppException {
		
		String result = StringUtils.toConvertString(inputStream);
		
		try {
			JSONObject object = new JSONObject(result);
			if (object != null) {
				
				JSONArray arr = (JSONArray) object.get("data");
				for (int i = 0; i < arr.length(); i++) {
					JSONObject obj = (JSONObject) arr.get(i);
					if (obj != null) {
						String id = obj.getString("propertyId");
						String val = obj.getString("propertyValue");
						if ("orderId".equals(id)) {
							orderId = val;
						} else if ("customer".equals(id)) {
							customer = val;
						} else if ("needDriver".equals(id)) {
							needDriver = !("false".equals(val));
						} else if ("comment".equals(id)) {
							taskComment.add(val);
							taskCompleteTime.add(WfUtil.parseJsonDate(obj.getString("time")));
						}
					}
				}
				
				hasDetail = true;
				//step = WfUtil.getStepStart(taskComment.size());
			}

		} catch (JSONException e) {
			throw AppException.json(e);
		} finally {
			inputStream.close();
		}
	}
}
